package cc.rboot.limit.grouper;

import cc.rboot._core.holder.AppContextHolder;
import cc.rboot.limit.core.AbstractAccessLimitGrouper;
import cc.rboot.token.core.ITokenAware;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

public class MethodTokenAccessLimitGrouper extends AbstractAccessLimitGrouper {

    @Override
    public String grouper(ProceedingJoinPoint joinPoint) throws Exception {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod = methodSignature.getMethod();

        ITokenAware tokenAware = AppContextHolder.getBean(ITokenAware.class);
        if(tokenAware==null) {
            throw new Exception("必须实现ITokenAware接口才能使用分组器：MethodTokenAccessLimitGrouper");
        }
        return joinPoint.getThis().getClass().getName() + "_" + targetMethod.getName() + "_" + tokenAware.token();
    }
}
